package model

import (
	"fmt"
	"time"

	"gitee.com/goframe/lt-collector/libs/libce"
	"gitee.com/goframe/lt-collector/libs/orma"
	"gitee.com/goframe/lt-collector/model/txffc"
)

type (
	TxData struct {
		OpenCode string
	}
	TXCollector struct {
		orma.BaseModel `xorm:"extends"`

		Gid     int64  `json:"gid" xorm:"notnull comment(彩种ID)"`
		LotCode string `json:"lot_code" xorm:"varchar(20) notnull comment(接口绑定彩种参数)"`
	}
)

func (t *TxData) GetOpenIssue() string {
	return ""
}

func (t *TxData) GetOpenCode() string {
	return t.OpenCode
}

func (t *TxData) GetOpenTime() orma.JSONTime {
	return orma.JSONTime(time.Now())
}

func (m *TXCollector) getByGid(gid int64) error {
	return libce.CacheBindModelByKey(m, fmt.Sprintf("gid:%d", gid), func() error {
		err := orma.NormalGet(m.Session().Where("gid=?", gid).Get(m))
		if err == orma.ErrOrmNotFound {
			err = fmt.Errorf("采集错误:Gid:[%d]未设置采集点参数", gid)
		}
		return err
	})
}

func (m *TXCollector) GetParam() (paramString string, err error) {
	return
}

func (m *TXCollector) GetData(apiURL string) (data RequestData, err error) {
	var openCode string
	for {
		if openCode, err = txffc.CalculateCode(); err == nil {
			break
		}
		log.Printf("访问[腾讯分分彩]接口失败:[%s];等待2秒后继续请求\n", err.Error())
		time.Sleep(time.Second * 2)
	}
	data = &TxData{OpenCode: openCode}
	return
}
